package com.kmu.kmucampus.adapter.treeViewAdapter;

import com.kmu.kmucampus.R;

import java.util.ArrayList;
import java.util.List;

import mytest.ssw.www.mylibrary.DeptNode;

/**
 * http://blog.csdn.net/lmj623565791/article/details/40212367
 * @author zhy
 *
 */
public class TreeHelper
{
	/**
	 * 传入我们的普通bean，转化为我们排序后的Node
	 *
	 * @param defaultExpandLevel
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static <T> List<DeptNode> getSortedNodes(List<DeptNode> nodes,
			int defaultExpandLevel) throws IllegalArgumentException,
			IllegalAccessException

	{
		List<DeptNode> result = new ArrayList<DeptNode>();
		nodes=formatNodes(nodes);

		// 拿到根节点
		List<DeptNode> rootNodes = getRootNodes(nodes);
		// 排序以及设置Node间关系
		for (DeptNode rootNode : rootNodes)
		{
			addNode(result, rootNode, defaultExpandLevel, 1);
		}

		return result;
	}

	/**
	 * 过滤出所有可见的Node
	 * 
	 * @param nodes
	 * @return
	 */
	public static List<DeptNode> filterVisibleNode(List<DeptNode> nodes)
	{
		List<DeptNode> result = new ArrayList<DeptNode>();

		for (DeptNode node : nodes)
		{
			// 如果为跟节点，或者上层目录为展开状态
			if (node.isRoot() || node.isParentExpand())
			{
				setNodeIcon(node);
				setSelectIcon(node);
				result.add(node);
			}
		}
		return result;
	}
	/**
	 * 过滤出所有可见的Node
	 *
	 * @param
	 * @return
	 */
	public static void updateSelectNode(DeptNode node)
	{
		List<DeptNode> result = new ArrayList<DeptNode>();


			//更新子、孙节点选中状态
			if(!node.isLeaf()){
				updateSelectChild(node);
			}
			//更新父节点
			if(!node.isRoot()){
				updateSelectParent(node);
			}
				setSelectIcon(node);
	//	result.add(node);

	}
	private static void  updateSelectParent(DeptNode child){

		DeptNode parent = child.getParent();

		int childrenSelectFlag = 0;
		List<DeptNode> children = parent.getChildren();
		for(DeptNode cNode : children){
			childrenSelectFlag += cNode.getSelectFlag();
		}
		if(childrenSelectFlag==children.size()*2){
			parent.setSelectFlag(2);
		}else if(childrenSelectFlag == 0){
			parent.setSelectFlag(0);
		}else
			parent.setSelectFlag(1);
			setSelectIcon(parent);
		if(!parent.isRoot()){
			updateSelectParent(parent);
		}

	}

	private static void  updateSelectChild(DeptNode parent){
		int parentSelectFlag = parent.getSelectFlag();
		List<DeptNode> children = parent.getChildren();
		for(DeptNode cNode : children){
			if(cNode.getSelectFlag()!=parentSelectFlag)
				cNode.setSelectFlag(parentSelectFlag);
			// 如果为展开状态
			if (parent.isExpand())
			{
				setSelectIcon(cNode);
			}

			if(!cNode.isLeaf()){
				updateSelectChild(cNode);
			}
		}
	}
	/**
	 * 将我们的数据转化为树的节点
	 *
	 * @return
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */


	//返回new node类型
	private static <T> List<DeptNode> formatNodes(List<DeptNode> nodes)
			throws IllegalArgumentException, IllegalAccessException
	{

		/**
		 * 设置Node间，父子关系;让每两个节点都比较一次，即可设置其中的关系
		 */
		for (int i = 0; i < nodes.size(); i++)
		{
			DeptNode n = nodes.get(i);
			for (int j = i + 1; j < nodes.size(); j++)
			{
				DeptNode m = nodes.get(j);
				if (m.getpNum().equals(n.getNum()))
				{
					n.getChildren().add(m);
					m.setParent(n);
				} else if (m.getNum().equals(n.getpNum()))
				{
					m.getChildren().add(n);
					n.setParent(m);
				}
			}
		}

		return nodes;
	}

	private static List<DeptNode> getRootNodes(List<DeptNode> nodes)
	{
		List<DeptNode> root = new ArrayList<DeptNode>();
		for (DeptNode node : nodes)
		{
			if (node.isRoot())
				root.add(node);
		}
		return root;
	}

	/**
	 * 把一个节点上的所有的内容都挂上去
	 */
	private static void addNode(List<DeptNode> nodes, DeptNode rootNode,
			int defaultExpandLeval, int currentLevel)
	{
		//加入根节点
		nodes.add(rootNode);
		if (defaultExpandLeval >= currentLevel)
		{
			rootNode.setExpand(true);
		}

		if (rootNode.isLeaf())
			return;
		for (int i = 0; i < rootNode.getChildren().size(); i++)
		{
			addNode(nodes, rootNode.getChildren().get(i), defaultExpandLeval,
					currentLevel + 1);
		}
	}

	/**
	 * 设置节点的图标
	 * 
	 * @param node
	 */
	private static void setNodeIcon(DeptNode node)
	{
		if (node.getChildren().size() > 0 && node.isExpand())
		{
//			Resources res = getResources();
//			Drawable drawable = res.getDrawable(R.drawable.myimage);

			node.setIcon(R.drawable.tree_ex);
		} else if (node.getChildren().size() > 0 && !node.isExpand())
		{
			node.setIcon(R.drawable.tree_ec);
		} else
			node.setIcon(-1);

	}
	/**
	 * 设置节点的图标
	 *
	 * @param node
	 */
	private static void setSelectIcon(DeptNode node)
	{

		if(node.getSelectFlag()==0){
			node.setSelectIcon(R.drawable.select_none);
		}else if(node.getSelectFlag()==1){
			node.setSelectIcon(R.drawable.select_half);
		}else {
			node.setSelectIcon(R.drawable.select_all);
		}
	}
}
